package edu.ccut.saturn.studio.wfg.dialog.vocationallogicdialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.wfg.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.wfg.wfgmodel.ExtendAttributeForLogicKey;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnAttribute;

public class SaturnVocationalLogicParameterAddDialog extends TitleAreaDialog {
	
	private ExtendAttributeForLogicKey extendAttributeForLogicKey;
	
	private List<SaturnAttribute> attributes;
	
	private Text idText, parameterText;
	
	private Label idLabel, parameterLabel;

	public SaturnVocationalLogicParameterAddDialog(Shell parentShell, List<SaturnAttribute> attributes) {
		super(parentShell);
		this.attributes = attributes;
		this.extendAttributeForLogicKey = new ExtendAttributeForLogicKey();
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages.getString("SaturnVocationalLogicParameterAddDialog.SetParameter"));    //$NON-NLS-1$
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		
		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING);
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		Composite area = (Composite) super.createDialogArea(parent);
		final Composite composite = new Composite(area, SWT.NONE);
		
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true);
		composite.setLayoutData(gd_composite);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		
		idLabel = new Label(composite, SWT.NONE);
		final GridData gd_idLabel = new GridData();
		gd_idLabel.verticalIndent = 5;
		idLabel.setLayoutData(gd_idLabel);
		idLabel.setText("ID");
		
		idText = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_idTextt = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_idTextt.verticalIndent = 5;
		idText.setLayoutData(gd_idTextt);
		idText.setText(String.valueOf(attributes.size() + 1));
		
		parameterLabel = new Label(composite, SWT.NONE);
		final GridData gd_parameterLabel = new GridData();
		gd_parameterLabel.verticalIndent = 5;
		parameterLabel.setLayoutData(gd_parameterLabel);
		parameterLabel.setText("parameter");
		
		parameterText = new Text(composite, SWT.BORDER);
		parameterText.setTextLimit(50);
		parameterText.setFocus();
		
		parameterText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if (!"".equals(parameterText.getText().trim() )) {
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				} 
				if ("".equals(parameterText.getText().trim())) {
					getButton(IDialogConstants.OK_ID).setEnabled(false);
					setErrorMessage(Messages.getString("SaturnVocationalLogicParameterAddDialog.ParamrterCannotEmpty"));  //$NON-NLS-1$
				}
			}	
		});	
		final GridData gd_parameterText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_parameterText.verticalIndent = 5;
		gd_idTextt.verticalIndent = 10;
		parameterText.setLayoutData(gd_parameterText);
		return area;
	}

	@Override
	protected Point getInitialSize() {
		return new Point(360, 220);
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if(buttonId == IDialogConstants.OK_ID){
			extendAttributeForLogicKey.setId(idText.getText().trim());
			extendAttributeForLogicKey.setParameter(parameterText.getText().trim());
			attributes.add(extendAttributeForLogicKey);
		}
		super.buttonPressed(buttonId);
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}
}
